﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text;
using System.IO;
using System.Xml.Linq;
using System.Xml;
using System.Linq;
using System.Collections.Generic;

namespace SilverlightBase.Classes
{
	public class Menu
	{
		/// <summary>
		/// Setup menu
		/// </summary>
		/// <param name="nodeId"></param>
		/// <param name="rootNodeId"></param>
		public static void BindMenu(int nodeId, int rootNodeId)
		{
			// Set currentnode, for menu click 
			((Page)Application.Current.RootVisual).currentNodeId = nodeId;

			// Remove all children in menu
			((Page)Application.Current.RootVisual).Menu.Children.Clear();

			// Bind menu
			BindMenuLevel(rootNodeId, 1, 0);
		}

		/// <summary>
		/// Bind each level of the menui
		/// </summary>
		/// <param name="renderNodeId">Node to display</param>
		/// <param name="level">Level on the menu</param>
		/// <param name="menuIndex">Index in the menu</param>
		/// <returns>Index in the mene, used for sub levels</returns>
		private static int BindMenuLevel(int renderNodeId, int level, int menuIndex)
		{
			// Read xml
			System.IO.TextReader textReader = new System.IO.StringReader(((Page)Application.Current.RootVisual).siteXml);
			XElement siteXmlDoc = XElement.Load(textReader);

			// Get page nodes
			var nodes = from node in siteXmlDoc.Descendants("node")
						where node.Parent.Attribute("id").Value == renderNodeId.ToString()
						select new string[] { node.Attribute("nodeName").Value, node.Attribute("id").Value, node.Parent.Attribute("id").Value };

			// Get blog nodes if target page is a blog page
			if (renderNodeId == ((Page)Application.Current.RootVisual).blogNodeId)
			{
				nodes = from node in siteXmlDoc.Descendants("node")
						where node.Parent.Attribute("id").Value == renderNodeId.ToString()
						orderby node.Attribute("id").Value descending
						select new string[] { node.Attribute("nodeName").Value, node.Attribute("id").Value, node.Parent.Attribute("id").Value };
			}

			// Loop nodes to be rendered
			foreach (string[] node in nodes)
			{
				if (((Page)Application.Current.RootVisual).blogPostNodeId != node[1].ToInt32())
				{
					// Set currentNodeId to first node in xml if site entry
					if (((Page)Application.Current.RootVisual).currentNodeId == ((Page)Application.Current.RootVisual).rootNodeId)
						((Page)Application.Current.RootVisual).currentNodeId = node[1].ToInt32();

					// Render level 1 menu items
					if (level == 1)
					{
						// Render selected and not selected
						if (((Page)Application.Current.RootVisual).currentNodeId == node[1].ToInt32() || SelctedItemIsChild(node[1].ToInt32()))
							((Page)Application.Current.RootVisual).Menu.Children.Add((Canvas)System.Windows.Markup.XamlReader.Load(Xaml.Menu.GetMenuItemXaml(Xaml.Menu.menuItemType.TopSelected, node[0], node[1].ToInt32(), menuIndex * 26, ((Page)Application.Current.RootVisual).selectedColour)));
						else
							((Page)Application.Current.RootVisual).Menu.Children.Add((Canvas)System.Windows.Markup.XamlReader.Load(Xaml.Menu.GetMenuItemXaml(Xaml.Menu.menuItemType.Top, node[0], node[1].ToInt32(), menuIndex * 26, ((Page)Application.Current.RootVisual).selectedColour)));
					}
					else
					{
						// Render selected and not selected
						if (((Page)Application.Current.RootVisual).currentNodeId == node[1].ToInt32())
							((Page)Application.Current.RootVisual).Menu.Children.Add((Canvas)System.Windows.Markup.XamlReader.Load(Xaml.Menu.GetMenuItemXaml(Xaml.Menu.menuItemType.SubSelected, node[0], node[1].ToInt32(), menuIndex * 26, ((Page)Application.Current.RootVisual).selectedColour)));
						else
							((Page)Application.Current.RootVisual).Menu.Children.Add((Canvas)System.Windows.Markup.XamlReader.Load(Xaml.Menu.GetMenuItemXaml(Xaml.Menu.menuItemType.Sub, node[0], node[1].ToInt32(), menuIndex * 26, ((Page)Application.Current.RootVisual).selectedColour)));
					}

					// Add mouse event
					((Canvas)((Page)Application.Current.RootVisual).Menu.Children[menuIndex]).MouseLeftButtonUp += new MouseButtonEventHandler(Events.Menu_Button_MouseLeftButtonUp);

					// Update index
					menuIndex++;

					// Render level 2
					if (level == 1 && (((Page)Application.Current.RootVisual).currentNodeId == node[1].ToInt32() || SelctedItemIsChild(node[1].ToInt32())))
						menuIndex = BindMenuLevel(node[1].ToInt32(), (level + 1), menuIndex);
				}
			}

			// Close reader
			textReader.Close();

			// Return index from sub levels
			return menuIndex;
		}

		/// <summary>
		/// Test if selected menu item is child
		/// </summary>
		/// <param name="parentNodeId">Parent node id</param>
		/// <returns>boolean</returns>
		private static bool SelctedItemIsChild(int parentNodeId)
		{
			bool isTheOne = false;

			// Read xml
			System.IO.TextReader textReader = new System.IO.StringReader(((Page)Application.Current.RootVisual).siteXml);
			XElement siteXmlDoc = XElement.Load(textReader);

			// Get nodes
			var nodes = from node in siteXmlDoc.Descendants("node")
						where node.Parent.Attribute("id").Value == parentNodeId.ToString()
						select new string[] { node.Attribute("id").Value };

			// Loop to test for child
			foreach (string[] node in nodes)
			{
				if (node[0].ToInt32() == ((Page)Application.Current.RootVisual).currentNodeId)
					isTheOne = true;
			}

			// Close reader
			textReader.Close();

			return isTheOne;
		}

		/// <summary>
		/// Events on the menu
		/// </summary>
		public static class Events
		{
			// On menu item click
			public static void Menu_Button_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
			{
				// Setup page
				((Page)Application.Current.RootVisual).SetupPage(Convert.ToInt32(((Canvas)sender).Tag));
			}
		}
	}
}
